﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ContactManager.Models;
using ContactManager.Models.Validation;
using ContactManager.Models.Entities;

namespace ContactManager.Controllers
{
    public class GroupController : Controller
    {
        private readonly IContactManagerService _service;
        private IValidationService _validationService = null;


        public GroupController(IContactManagerService service, IValidationService validationService)
        {
            _service = service;
            _validationService = validationService;
        }

       // public ActionResult Index()
        //{
       //     return View(_service.ListGroups());
        //}
        public ActionResult Index(int? page)
        {
             //_service = new ContactManagerService(new ModelStateWrapper(this.ModelState));
            //_service = new ContactManagerService();
            const int pageSize = 5;
            var allGroups = _service.FindAllGroups().ToList();
            var paginatedGroups = new PaginatedList<ContactGroup>(allGroups, page ?? 0, pageSize);


            return View(paginatedGroups);
        }
        [HttpGet]
        public ActionResult Create()
        {
            
            return View("Create");
        }
        [Authorize,HttpPost]
        public ActionResult Create([Bind(Exclude="Id")]ContactGroup groupToCreate)
        {
            if (_service.CreateGroup(groupToCreate))
                return RedirectToAction("Index");
            return View("Index");
        }
        [AcceptVerbs(HttpVerbs.Delete)]
        [ActionName("DeleteGroupViaAjax")]
        public ActionResult AjaxDelete(System.Guid id)
        {
            // Get contact and group
            var groupToDelete = _service.GetGroup(id);
            //var selectedGroup = _service.GetGroup(contactToDelete.ContactGroup.Id);

            const int pageSize = 5;
           

            

            // Ajax Request
            if (Request.IsAjaxRequest())
            {
                // Delete from database
                _service.DeleteGroup(groupToDelete);

               

                // Return Contact List
                //return View(paginatedGroups);
               // return PartialView("GroupList", paginatedGroups); 

            }
            var allGroups = _service.FindAllGroups().ToList();
            var paginatedGroups = new PaginatedList<ContactGroup>(allGroups, 0, pageSize);

            return PartialView("GroupList", paginatedGroups); 

        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(ContactGroup groupToDelete)
        {
            if (_service.DeleteGroup(groupToDelete))
                return RedirectToAction("Index");
            return View("Delete", _service.GetGroup(groupToDelete.Id));
        }
        [HttpPost, Authorize]
        public ActionResult Edit(int id, ContactGroup group)
        {
            
            //VALIDATE CONTACTGROUP before sending it to service layer
            ValidationState vst = _validationService.Validate<ContactGroup>(group);
            try
            {
                // TODO: Add update logic here
                //UpdateModel(contact);
                _service.EditGroup(group);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }
        [Authorize]
        public ActionResult Edit(System.Guid id)
        {

            ContactGroup group = _service.GetGroup(id);
                return View(new GroupFormViewModel(group));
        }


    }
}
